package com.supercube.system.controller;

import com.github.pagehelper.PageInfo;
import com.supercube.common.annotation.Log;
import com.supercube.common.config.Constant;
import com.supercube.common.controller.BaseController;
import com.supercube.common.utils.R;
import com.supercube.sport.yoga.utils.ResultInfo;
import com.supercube.system.domain.RoleDO;
import com.supercube.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/sys/role")
@Controller
public class RoleController extends BaseController {
    String prefix = "system/role";
    @Autowired
    RoleService roleService;

    //@RequiresPermissions("sys:role:role")
    @GetMapping()
    String role() {
        return prefix + "/role";
    }

    //@RequiresPermissions("sys:role:role")
    @PostMapping("/list")
    @ResponseBody()
    ResultInfo list(@RequestBody Map<String,Object> map) {
        List<RoleDO> roles = roleService.list(map);
        PageInfo<RoleDO> pageInfo = new PageInfo<>(roles);
        return ResultInfo.success(pageInfo);
    }

    @Log("添加角色")
    //@RequiresPermissions("sys:role:add")
    @GetMapping("/add")
    String add() {
        return prefix + "/add";
    }

    @Log("编辑角色")
    //@RequiresPermissions("sys:role:edit")
    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Long id, Model model) {
        RoleDO roleDO = roleService.get(id);
        model.addAttribute("role", roleDO);
        return prefix + "/edit";
    }

    @Log("保存角色")
    //@RequiresPermissions("sys:role:add")
    @PostMapping("/save")
    @ResponseBody()
    R save(@RequestBody RoleDO role) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        if (roleService.save(role) > 0) {
            return R.ok();
        } else {
            return R.error(1, "保存失败");
        }
    }

    @Log("更新角色")
    //@RequiresPermissions("sys:role:edit")
    @PostMapping("/update")
    @ResponseBody()
    R update(@RequestBody RoleDO role) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        if (roleService.update(role) > 0) {
            return R.ok();
        } else {
            return R.error(1, "保存失败");
        }
    }

    @Log("删除角色")
    //@RequiresPermissions("sys:role:remove")
    @PostMapping("/remove")
    @ResponseBody()
    R save(Long id) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        if (roleService.remove(id) > 0) {
            return R.ok();
        } else {
            return R.error(1, "删除失败");
        }
    }

    //@RequiresPermissions("sys:role:batchRemove")
    @Log("批量删除角色")
    @PostMapping("/batchRemove")
    @ResponseBody
    R batchRemove(@RequestParam("ids[]") Long[] ids) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        int r = roleService.batchremove(ids);
        if (r > 0) {
            return R.ok();
        }
        return R.error();
    }

    @Log("菜单关联角色")
    @PostMapping("/saveMenuRole")
    @ResponseBody
    public ResultInfo saveMenuRole(@RequestBody Map<String,Object> map ){
        Integer r = roleService.saveMenuRole(map);
        return ResultInfo.success("保存成功");
    }

    @Log("查询当前已经绑定的菜单")
    @PostMapping("/queryMenuByRole")
    @ResponseBody
    public ResultInfo queryMenuByRole(@RequestParam("roleId") String roleId ){
        // 查询当前已绑定的菜单
        List<Integer> menuIds = roleService.queryMenuByRole(roleId);
        // 查询当前已绑定的按钮
        List<Integer> buttonIds = roleService.queryButtonByRole(roleId);
        Map<String,Object> map = new HashMap<>();
        map.put("menuIdList",menuIds);
        map.put("buttonIdList",buttonIds);
        return ResultInfo.success(map);
    }
    @Log("根据角色查询当前已经绑定的用户")
    @PostMapping("/queryUserByRole")
    @ResponseBody
    public ResultInfo queryUserByRole(@RequestParam("roleId") String roleId ){
        List<Integer> userIds = roleService.queryUserByRole(roleId);
        return ResultInfo.success(userIds);
    }
    @Log("将用户绑定到角色")
    @PostMapping("/bindUserToRole")
    @ResponseBody
    public ResultInfo bindUserToRole(@RequestBody Map<String,Object> map  ){
        roleService.bindUserToRole(map);
        return ResultInfo.success("保存成功");
    }
}
